#!perl
use strict;
use Getopt::Long;
use lib '/homes/smandal/work/dcg/om/graph';
use JNPR::Utils;

my $S = 'https://svl-svn.juniper.net/svn/junos-2009';
my $g_sql_out = [];
my $opts = {};
GetOptions ($opts, 'db=s');
my $db = 'db';
if (exists $opts->{db}) {
    $db = $opts->{db};
}
JNPR::Utils::mk_tables($db);



my $subdir;
my %subdirs = ();

if (scalar @ARGV) {
    dig_specific_versions();
    exit;
}
else {
    dig_remaining_versions();
}


#
# --------------------------------------------------------------------
# --------------------------------------------------------------------
sub dig_remaining_versions
{
    my ($svn_hi_rev, $svn_lo_rev) = JNPR::Utils::get_counts($db, 'svn_hi_rev', 'svn_lo_rev');

    my $dig_success = 1;
    my $v = $svn_hi_rev;
    while ($dig_success) {
        if (-e 'kill') {
            unlink 'kill';
            JNPR::Utils::set_counts($db, 'svn_hi_rev', $v);
            exit;
        }
        $v++;
        $dig_success = dig_version($v);
    }
    JNPR::Utils::set_counts($db, 'svn_hi_rev', $v - 1);

    $dig_success = 1;
    $v = $svn_lo_rev;
    while ($dig_success) {
        if (-e 'kill') {
            unlink 'kill';
            JNPR::Utils::set_counts($db, 'svn_lo_rev', $v);
            exit;
        }
        $v--;
        $dig_success = dig_version($v);
    }
    JNPR::Utils::set_counts($db, 'svn_lo_rev', $v + 1);
}

#
# --------------------------------------------------------------------
# --------------------------------------------------------------------
{
my $n;
my $lastrev;
sub dig_version {
    my $v = shift;

    my $cmd;
    $n = 0 unless (defined $n);
    $lastrev = 0 unless (defined $lastrev);
    if ($v > $lastrev) {
        my @info = `svn info $S`;
        # Revision: 455745
        chomp $info[4];
        ($lastrev) = $info[4] =~ /(\d+)$/;
        return 0 if($v > $lastrev);
    }
    return 0 if($v < 0);

    print "$v\n";
    ($subdir) = "00$v" =~ /(..)$/;
    if (! exists $subdirs{$subdir}) {
        system "mkdir -p $subdir";
        $subdirs{$subdir} = 1;
    }
    open F, ">$subdir/${v}-rlog.txt" or die;
    open CMD, "svn log -v -r $v $S|" or die;

    # Read 10 lines from svn "log -v" on put it in @rlog
    my @rlog = ();
    my $nlines = 0;
    while ((my $line = <CMD>) && ($nlines <10)) {
        push @rlog, $line;
        print F $line;
    }
    close CMD;
    close F;

    JNPR::Utils::scan_rlog_for_merge_or_branch(\@rlog);

    $n++;
    if ($n % 1000 == 0) {
        system 'date; echo sleeping...';
        sleep 1 * 60;
    }

    return 1;
}
}

#
# --------------------------------------------------------------------
# --------------------------------------------------------------------
sub dig_specific_versions
{
    if ($ARGV[0] eq '-') {
        while (my $v = <STDIN>) {
            if (-e 'kill') {
                unlink 'kill';
                exit;
            }
            chomp $v;
            dig_version($v);
        }
    }
    else {
        for my $v (@ARGV) {
            if (-e 'kill') {
                unlink 'kill';
                exit;
            }
            if ($v =~ /^\d+$/) {
                dig_version($v);
            }
        }
    }
}


